<?php

// @filename   mnetbar.php

class Mnetbar extends Model {

    function Mnetbar() {
        parent::Model();
    }

    function get_area_list(){
    	$query = $this->db->get('netbar_areas');
    	return $query->result_array();
    }
    
	function get_area_array(){
    	$query = $this->db->get('netbar_areas');
    	$res = $query->result_array();
    	$areas = array();
    	foreach($res as $arr){
    		$areas[$arr['area_id']] = $arr['area_name'];
    	}
    	return $areas;
    }

    function get_index_area() {
        $areas = self::get_area_list();
        $arr = array();
        foreach($areas as $area){
            $area['state'] = self::get_netbar_count($area['area_id']);
            $arr[] = $area;
        }
        return $arr;
    }

    function get_netbar_by_area($area_id,$offset,$limit){
    	$this->db->limit($limit,$offset);
    	$query = $this->db->get_where('netbars',array('area_id'=>$area_id));
    	return $query->result_array();
    }

    function get_area_info($area_id){
    	$query = $this->db->get_where('netbar_areas',array('area_id'=>$area_id));
    	return $query->row_array();
    }

    function get_area_name($area_id){
    	$query = $this->db->get_where('netbar_areas',array('area_id'=>$area_id));
    	$row = $query->row_array();
        return $row['area_name'];
    }

    function get_netbar_count($area_id){
    	$this->db->where('area_id',$area_id);
    	return $this->db->count_all_results('netbars');
    }

    function get_agent_bar_count($agent_no){
        $this->db->where('agent',$agent_no);
    	return $this->db->count_all_results('netbars');
    }

    function get_agent_bars($agent_no,$offset=0,$limit=10){
        $this->db->where('agent',$agent_no);
        $this->db->limit($limit,$offset);
        $query = $this->db->get('netbars');
        return $query->result_array();
    }

    function get_areas($limit='',$offset=0){
        if($limit !== ''){
            $this->db->limit($limit,$offset);
        }
        $query = $this->db->get('netbar_areas');
        return $query->result_array();
    }

    function get_area_count(){
    	return $this->db->count_all_results('netbar_areas');
    }

    function insert_new_bar($values){
        return $this->db->insert('netbars',$values);
    }

    function get_bar_info($id){
        $query = $this->db->get_where('netbars',array('id'=>$id));
    	return $query->row_array();
    }

    function get_barinfo_uid($uid){
        $query = $this->db->get_where('netbars',array('bar_uid'=>$uid));
    	return $query->row_array();
    }

    function update_bar_info($id,$values){
        return $this->db->update('netbars',$values,array('id'=>$id));
    }

    function delete_bar($id){
        return $this->db->delete('netbars',array('id'=>$id));
    }

    function insert_new_area($values){
        return $this->db->insert('netbar_areas',$values);
    }

    function delete_area($id){
        return $this->db->delete('netbar_areas',array('area_id'=>$id));
    }

    function update_area_info($area_id,$values){
        return $this->db->update('netbar_areas',$values,array('area_id'=>$area_id));
    }

}
?>